package com.finance.services;

import com.finance.model.UserAccount;
import com.finance.model.types.UserAccountTypeEnum;
import com.finance.services.dao.IUserAccountDAO;
import java.util.List;
import javax.inject.Inject;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Visual556
 */
@Service(value = UserAccountService.NAME)
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Transactional
public class UserAccountService implements IUserAccountService {

    public static final String NAME = "userAccountService";

    @Inject
    private IUserAccountDAO userAccountDAO;

    @Override
    public void register(UserAccount userAccount) throws Exception {
        userAccount.setType(UserAccountTypeEnum.REGULAR);
        userAccountDAO.create(userAccount);
    }

    @Override
    public UserAccount login(UserAccount userAccount) throws Exception {
        UserAccount result = userAccountDAO.read(userAccount.getUsername());
        if (result.getPassword().equals(userAccount.getPassword())) {
            return result;
        }
        throw new Exception("Wrong password.");
    }

    @Override
    public List<UserAccount> loadAllUsers() throws Exception {
        return userAccountDAO.read();
    }
}
